using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 嘲讽攻击策略 - 吸引范围内所有敌人的仇恨，让它们优先攻击施法者
/// </summary>
public class TauntStrategy : AttackStrategyBase
{
    private Coroutine m_TauntCoroutine;
    private float m_LastTauntTime;

    public TauntStrategy(SkillData data)
    {
        AttackData = data;
    }

    public override void Execute(Entity target)
    {
        base.Execute(target);

        // 如果已经在执行，则刷新时间
        if (m_TauntCoroutine != null)
        {
            return;
        }

        // 开始持续嘲讽
        m_TauntCoroutine = Owner.StartCoroutine(TauntRoutine());
    }

    /// <summary>
    /// 嘲讽协程 - 持续对范围内的敌人施加嘲讽效果
    /// </summary>
    private IEnumerator TauntRoutine()
    {
        while (Owner != null && Owner.HealthComponent != null && Owner.HealthComponent.CurHealth > 0)
        {
            // 应用嘲讽效果
            ApplyTauntToEnemies();

            // 等待间隔时间
            yield return new WaitForSeconds(AttackData.Interval);
        }

        m_TauntCoroutine = null;
    }

    /// <summary>
    /// 对范围内的所有敌人施加嘲讽效果
    /// </summary>
    private void ApplyTauntToEnemies()
    {
        if (Owner == null || Owner.AttackComponent == null)
        {
            return;
        }

        // 查找范围内的所有敌人
        var enemies = Owner.AttackComponent.FindEnemys();
        
        if (enemies == null || enemies.Count == 0)
        {
            return;
        }

        // 从Effects中获取TauntBuffData
        BuffData tauntBuffData = null;
        
        if (AttackData.Effects != null && AttackData.Effects.Count > 0)
        {
            foreach (var effect in AttackData.Effects)
            {
                if (effect is AddBuffEffectData addBuffEffect)
                {
                    if (addBuffEffect.BuffData != null && addBuffEffect.BuffData.BuffType == BuffType.Taunt)
                    {
                        tauntBuffData = addBuffEffect.BuffData;
                        break;
                    }
                }
            }
        }

        if (tauntBuffData == null)
        {
            Logger.LogWarning("TauntStrategy", "未找到TauntBuffData配置");
            return;
        }

        // 对每个敌人施加嘲讽Buff
        foreach (var enemy in enemies)
        {
            if (enemy == null || enemy.HealthComponent == null || enemy.HealthComponent.IsDead)
            {
                continue;
            }

            // 检查敌人是否有BuffComponent
            if (enemy.BuffComponent == null)
            {
                continue;
            }

            // 应用嘲讽Buff
            enemy.BuffComponent.AddBuff(tauntBuffData, Owner, enemy);
            
            Logger.Log("TauntStrategy", $"{Owner.name} 对 {enemy.name} 施加了嘲讽效果");
        }
    }

    public override void Cancel()
    {
        base.Cancel();
        
        if (m_TauntCoroutine != null && Owner != null)
        {
            Owner.StopCoroutine(m_TauntCoroutine);
            m_TauntCoroutine = null;
        }
    }
}



